#include "DictionaryTools.h"

#include "SQLTools.h"

extern sqlite3*	g_pDatabase;

//#define STRNCMP(a, b, c) ((!a || !b) ? (!a ? -1:1) : strncmp(a,b,c))

//int STRNCMP(const char *first,const char *last,int count)
//{
//	if (!count)
//		return (0);
//	
//	if (!first)
//		return -1;
//	if (!last)
//		return 1;
//	
//	while (--count && *first && *first == *last)
//		first++, last++;
//	return (*(unsigned char *) first - *(unsigned char *) last);
//}


FILE_DATA* DictionaryTools_ReadVerData()
{
	return FileTools_ReadDataEx(DICTIONARY_VER);
}

void DictionaryTools_WriteVerData(char *pData, int nSize)
{
	FileTools_WriteDataEx(DICTIONARY_VER, pData, nSize);
}

//TBOOL codeIsHave(char *pTemp)
//{
//	TBOOL bRet;
//	
//	sqlite3_stmt *statement;
//	
//	char sSql[256];
//	
//	char cMarket;
//	char sCode[8];
//	
//	cMarket = pTemp[1];
//	
//	memset(sCode, 0, sizeof(sCode));
//	memcpy(sCode,pTemp+3,6);
//	
//	bRet = FALSE;
//	
//	memset(sSql, 0, sizeof(sSql));
//	
//	sprintf(sSql,"select * from qdic where code='%s' and marketid=%d",sCode,cMarket);
//	if(sqlite3_prepare_v2(g_pDatabase, sSql, -1, &statement, NULL) == SQLITE_OK)
//	{
//		bRet = (sqlite3_step(statement) == SQLITE_ROW);
//		sqlite3_finalize (statement);
//	}
//	return bRet;
//}

void replace(char *strbuf, char *sstr, char *dstr)    /*用s2串替换s中的s1串*/
{  
    char *p;
    int nPos;
    int nLen;
    char strDbuf[512];
    char *pTemp;
    
    if ((strbuf == NULL)||(sstr == NULL)||(dstr == NULL))
        return;
    
    pTemp=strbuf;
    nPos = 0;
    nLen = strlen(sstr);
    memset(strDbuf, 0, sizeof(strDbuf));
	
    while((p = strstr(strbuf, sstr))!=NULL)
    {
        strncpy(strDbuf+strlen(strDbuf), strbuf, p-strbuf);
        strcat(strDbuf, dstr);
        nPos = p-strbuf+nLen;
        strbuf+=nPos;
    }
    strcat(strDbuf+strlen(strDbuf), strbuf);
    
    strcpy(pTemp, strDbuf);
}

void insertCode( char *pTemp)
{
	//添加
	char cMarket;
	char cType;
	char sCode[8];
	char sName[32];
	char sAbb[8];
	
	sqlite3_stmt *insert_statement;
	
	memset(sCode, 0, sizeof(sCode));
	memset(sName, 0, sizeof(sName));
	memset(sAbb, 0, sizeof(sAbb));
	
	cMarket = pTemp[1];
	cType = pTemp[2];
	memcpy(sCode,pTemp+3,6);
	memcpy(sName,pTemp+9,24);
	memcpy(sAbb, pTemp+33,6);
	
  char sSql[256];
  memset(sSql, 0, sizeof(sSql));
  //strcpy(sSql, "replace into qdic (marketid,type,code,name,abb) values(?,?,?,?,?)");
  
  //strcpy(sName, "abc'def'123");
  
  //过滤特殊字符
  replace(sCode,"'","''");
  replace(sName,"'","''");
  replace(sAbb,"'","''");
  
  sprintf(sSql, "replace into qdic VALUES (%d, %d,\"%s\",\"%s\",\"%s\")", cMarket, cType, sCode, sName, sAbb);
  
//  //Test
//    printf("\nreplace code:%s\tname:%s",sCode,sName);
//  ////
  
	//    NSString *snStr;
	//    snStr = [NSString stringWithUTF8String:sSql];
	//    snStr = [snStr stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
	//    strcpy(sSql, (const char*)[snStr UTF8String]);
    //
	if (sqlite3_prepare_v2(g_pDatabase, sSql, -1, &insert_statement, NULL) != SQLITE_OK)
	{
		printf("\n insert error");
	}
	
    
	
	//	
	//	if (sqlite3_bind_int(insert_statement, 1, cMarket) != SQLITE_OK)
	//	{
	//		printf("\ninsert error market");
	//	}
	//	if (sqlite3_bind_int(insert_statement, 2, cType) != SQLITE_OK)
	//	{
	//		printf("\ninsert error type");
	//	}
	//	if (sqlite3_bind_text(insert_statement, 3, sCode, strlen(sCode), NULL)!= SQLITE_OK)
	//	{
	//		printf("\ninsert error code");
	//	}
	//	if (sqlite3_bind_text(insert_statement, 4, sName, strlen(sName), NULL)!= SQLITE_OK)
	//	{
	//		printf("\ninsert error name");
	//	}
	//	if (sqlite3_bind_text(insert_statement, 5, sAbb, strlen(sAbb), NULL)!= SQLITE_OK)
	//	{
	//		printf("\ninsert error abb");
	//	}
	//printf("\ninsert ret:%d",);
    sqlite3_step(insert_statement);
	//sqlite3_reset(insert_statement);
}

//void updateCode(sqlite3_stmt *update_statement, char *pTemp)
//{
//	char sSql[256];
//	
//	//添加
//	char cMarket;
//	char cType;
//	char sCode[8];
//	char sName[32];
//	char sAbb[8];
//	
//	memset(sCode, 0, sizeof(sCode));
//	memset(sName, 0, sizeof(sName));
//	memset(sAbb, 0, sizeof(sAbb));
//	
//	cMarket = pTemp[1];
//	cType = pTemp[2];
//	memcpy(sCode,pTemp+3,6);
//	memcpy(sName,pTemp+9,24);
//	memcpy(sAbb, pTemp+33,6);
//	sprintf(sSql,"update qdic set type=?,name=?,abb=? where marketid=%d and code='%s'", cMarket, sCode);
//	
//	printf("\nupdate code:%s\tname:%s",sCode,sName);
//	
//	if (sqlite3_prepare_v2(g_pDatabase, sSql, -1, &update_statement, NULL) != SQLITE_OK)
//	{
//		printf("\n update error:%s",sqlite3_errmsg(g_pDatabase));
//	}
//	if (sqlite3_bind_int(update_statement, 1, cType) != SQLITE_OK)
//	{
//		printf("\nupdate error type:%s",sqlite3_errmsg(g_pDatabase));
//	}
//	if (sqlite3_bind_text(update_statement, 2, sName, strlen(sName), NULL)!= SQLITE_OK)
//	{
//		printf("\nupdate error name:%s",sqlite3_errmsg(g_pDatabase));
//	}
//	if (sqlite3_bind_text(update_statement, 3, sAbb, strlen(sAbb), NULL)!= SQLITE_OK)
//	{
//		printf("\nupdate error abb:%s",sqlite3_errmsg(g_pDatabase));
//	}
//	printf("\nupdate ret:%d",sqlite3_step(update_statement));
//	sqlite3_reset(update_statement);
//}

void deleteCode(char *pTemp)
{
	char sSql[256];
	
	char cMarket;
	char cType;
	char sCode[8];
	char sName[32];
	char sAbb[8];
	
	sqlite3_stmt *delete_statement;
	
	memset(sCode, 0, sizeof(sCode));
	memset(sName, 0, sizeof(sName));
	memset(sAbb, 0, sizeof(sAbb));
	
	//删除
	cMarket = pTemp[1];
	cType = pTemp[2];
	memcpy(sCode,pTemp+3,6);
	memcpy(sName,pTemp+9,24);
	memcpy(sAbb, pTemp+33,6);
	sprintf(sSql,"delete from qdic where marketid=%d and code='%s'", cMarket, sCode);
	//printf("\ndelete code:%s\tname:%s",sCode,sName);
	if (sqlite3_prepare_v2(g_pDatabase, sSql, -1, &delete_statement, NULL) != SQLITE_OK)
	{
		printf("\ndelete data error.");
	}
	
	sqlite3_step(delete_statement);
	//sqlite3_reset(delete_statement);
}

TBOOL g_bDTLock = FALSE;

TBOOL DictionaryTools_Lock()
{	
	while (g_bDTLock) {
		SLEEP(10);
	}
	
 	return g_bDTLock = TRUE;
}

TBOOL DictionaryTools_UnLock()
{
	g_bDTLock = FALSE;
	return TRUE;
}

void DictionaryTools_DeleteSql(){
    sqlite3_stmt *delete_statement;
    
    char sSql[256];
    
    sprintf(sSql,"delete from qdic");
    //printf("\ndelete code:%s\tname:%s",sCode,sName);
    if (sqlite3_prepare_v2(g_pDatabase, sSql, -1, &delete_statement, NULL) != SQLITE_OK)
    {
        printf("\ndelete data error.");
    }
    
    sqlite3_step(delete_statement);
}


void DictionaryTools_UpData(char *pData, int nSize)
{
    DictionaryTools_Lock();
    
	char *pTemp;
	int nNum;
	
	//sqlite3_stmt *update_statement;
	
	
	pTemp = pData;
	nNum = nSize/(DICTIONARY_CODE_INFO_SIZE+1);
	
	//printf("\n DictionaryTools_UpData start");
	sqlite3_exec(g_pDatabase,"BEGIN TRANSACTION",0,0,0); 
	
	int i;
	for (i=0; i<nNum; i++)
	{
		//对数据字典进行操作
		//memset(sSql, 0, sizeof(sSql));
		
		
		if (pTemp[0]==DICTIONARY_ADD || pTemp[0]==DICTIONARY_UP)
		{
            insertCode( pTemp);
		}
		else if (pTemp[0]==DICTIONARY_DEL)
		{
			//删除
			deleteCode( pTemp);
		}
		
		pTemp+=DICTIONARY_CODE_INFO_SIZE+1;
	}
	
	sqlite3_exec(g_pDatabase,"COMMIT",0,0,0); 
    
    DictionaryTools_UnLock();
    
    printf("\n DictionaryTools_UpData end nNum:%d",nNum);
}
